<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- //卡片区域 -->
      <el-card class="tree-card">
        <!-- 公司名称 -->
        <TreeTools :node-data="company" :is-root="true" />
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template v-slot="{ data }">
            <TreeTools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="AddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
        <AddDept
          ref="addDeptDialog"
          :depts-list="deptsList"
          :node-data="nodeData"
          :show-dialog.sync="showDialog"
          @add-success="getDepartments"
        />
        <el-card v-loading="loading" class="tree-card" />
      </el-card></div>
  </div>
</template>

<script>
// 导入组件对象
import AddDept from './components/add-dept.vue'
import TreeTools from './components/tree-tools.vue'
import { tranListToTreeData } from '@/utils/index'
import { reqGetDepartments } from '@/api/departments'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      list: [],
      showDialog: false,
      loading: false,
      nodeData: {},
      deptsList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { data: res } = await reqGetDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.list = tranListToTreeData(res.depts, '')
      this.deptsList = res.depts
      this.loading = false
    },
    // 处理添加操作, 显示弹层
    AddDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
    },
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$nextTick(() => {
        this.$refs.addDeptDialog.getDepartmentDetail()
      })
    }
  }
}
</script>
<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
